package xyz.cerberu.cet.search.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.cerberu.cet.anime.domain.Anime;
import xyz.cerberu.cet.global.annotation.JsonFilter;
import xyz.cerberu.cet.global.domain.Result;
import xyz.cerberu.cet.global.utils.ResultUtils;
import xyz.cerberu.cet.resource.domian.Resource;
import xyz.cerberu.cet.search.server.SearchService;

import java.util.List;

/**
 * @author β世界
 * Created on 21:20
 * @Description 搜索引擎控制器
 */
@Controller
public class SearchControl {
    private final SearchService searchService;

    @Autowired
    public SearchControl(SearchService searchService) {
        this.searchService = searchService;
    }

    /**
     * @author β世界 on 11:20 2020/4/29
     * @Description 搜索所有相关的资源和动漫
     * @param search:搜索信息
     * @return com.Cet.common.domain.Result
     **/
    @JsonFilter(type = Anime.class,include = "ani_id,cover,anime_name")
    @ResponseBody
    @RequestMapping(value = "/searchAnime",method = RequestMethod.POST)
    public Result<List<Anime>> searchAnime(String search,
                                              @RequestParam(required = false,value = "pageNub",defaultValue = "1") int pageNub){
        return ResultUtils.genSuccessResult(searchService.searchAnime(search, pageNub));
    }

    @JsonFilter(type = Resource.class,filter = "ani_id,res_id")
    @ResponseBody
    @RequestMapping(value = "/searchResource",method = RequestMethod.POST)
    public Result<List<Resource>> searchResource(String search,
                                 @RequestParam(value = "pageNub",defaultValue = "1") int pageNub){
        return ResultUtils.genSuccessResult(searchService.searchResource(search,pageNub));
    }

}
